<?php

require __DIR__ . '/../vendor/autoload.php';

$app = require_once __DIR__ . '/../bootstrap/app.php';

$app->make(\App\Console\Kernel::class)->bootstrap();

/* ============== Write Codes Below ================= */

// 转售商品类型
echo '>> 转售商品类型处理' . PHP_EOL;
\App\Models\Product::query()->where('resale_user_id', '>', 0)->update([
    'type' => \App\Enums\ProductType::RESALE,
]);

echo '>> 商品存证处理' . PHP_EOL;
\App\Models\ProductToken::query()
    ->where('order_id', '>', 0)
    ->whereNull('lock_type')->whereNull('lock_refer_id')
    ->update([
        'lock_type' => \App\Enums\TokenLockType::ORDER,
        'lock_refer_id' => DB::raw('order_id'),
    ]);

echo '>> 资产迁移' . PHP_EOL;
$orderAssets = \App\Models\OrderAsset::all();
foreach ($orderAssets as $orderAsset) {
    $asset = \App\Models\Asset::query()->where('source_type', \App\Enums\AssetSourceType::ORDER)
        ->where('token_id', $orderAsset->token_id)
        ->where('source_id', $orderAsset->order_id)->first();
    if (!$asset) {
        $asset = new \App\Models\Asset();
        $asset->user_id = $orderAsset->user_id;
        $asset->source_id = $orderAsset->order_id;
        $asset->source_type = \App\Enums\AssetSourceType::ORDER();
        $asset->contract_address = $orderAsset->contract_address;
        $asset->token_id = $orderAsset->token_id;
        $asset->trans_hash = $orderAsset->trans_hash;
        $asset->asset_status = $orderAsset->asset_status;
        $asset->save();
    }
}
